#include<string>
#include<unordered_map>
#include<fstream>
#include<iostream>

#include"Log.hpp"

using namespace LogModule;

const std::string defaultdict="./dict.txt";
const std::string sep=": ";

class Dict
{
public:
    Dict(const std::string&path=defaultdict):_dictpath(path)
    {

    }

    void DictLoad()
    {
        std::ifstream in;
        in.open(_dictpath,std::ios_base::in);
        if(!in.is_open())
        {
            LOG(LogLevel::FATAL)<<"Dict load failed";
            exit(1);
        }

        std::string line;
        while(std::getline(in,line))
        {
            size_t pos=line.find(sep);
            if(pos==std::string::npos)
            {
                LOG(LogLevel::DEBUG)<<"解析失败"<<line;
                continue;
            }

            std::string English=line.substr(0,pos);
            std::string Chinese=line.substr(pos+sep.size());
            if(English.empty()||Chinese.empty())
            {
                LOG(LogLevel::DEBUG)<<"无效:"<<line;
                continue;
            }
            _dict[English]=Chinese;
        }
    }

    std::string Translate(const std::string&word)
    {
        LOG(LogLevel::DEBUG)<<"进入翻译模块";
        auto iter=_dict.find(word);
        if(iter==_dict.end())
        {
            LOG(LogLevel::DEBUG)<<"无效输入:"<<word;
            return "";
        }

        return iter->second;
    }

    ~Dict()
    {
        
    }

private:
    std::string _dictpath;
    std::unordered_map<std::string,std::string> _dict;
};